﻿using NFine.Application.SSO;
using NFine.Code;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NFine.Web.Areas.SSO.Controllers
{
    public class SsoAuthenticationController : ControllerBase
    {
        SsoAuthticationApp ssoAuthenticationApp = new SsoAuthticationApp();
        NFineBaseEntities db = new NFineBaseEntities();
        /// <summary>
        /// 获取数据列表
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJson(Pagination pagination, string keyword)
        {
            var data = new
            {
                rows = ssoAuthenticationApp.GetList(pagination, keyword),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetList()
        {
            var data = ssoAuthenticationApp.GetList();
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetEnableList()
        {
            var data = ssoAuthenticationApp.GetList().Where(p => p.Sa_Enable == 0);
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJson(int keyValue)
        {
            var data = ssoAuthenticationApp.GetForm(keyValue);
            //var data = db.Sso_Authtication.Find(keyValue);
            return Content(data.ToJson());
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult Add(Sso_Authtication sso_Authtication, string keyValue)
        {
            if (sso_Authtication != null)
            {
                if (sso_Authtication.Sa_Id == 0)
                {
                    sso_Authtication.Sa_Create_Time = DateTime.Now;
                    sso_Authtication.Sa_Update_Time = DateTime.Now;
                    db.Sso_Authtication.Add(sso_Authtication);
                    db.SaveChangesAsync();
                    return Success("添加成功。");
                }
                else
                {
                    sso_Authtication.Sa_Update_Time = DateTime.Now;
                    db.Entry(sso_Authtication).State = EntityState.Modified;
                    db.SaveChangesAsync();
                    return Success("修改成功。");
                }

            }
            else
            {
                return Error("操作失败");
            }
        }
    }
}
